/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mocks;

/**
 *
 * @author Alexx
 */

import server.*;
import engine.Singleton;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;
import java.sql.SQLException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import listener.MyListener;
import listener.MyrealListener;
import listener.Proxy;
import org.json.simple.parser.ParseException;
import protocol.Protocol;



public class OneThreadMock extends Thread
{
  
    private Socket socket;
    private BufferedReader in;
    private PrintWriter out;
    private static List<Player> clients;
    public MyListener listener;

  
    public Player p;
   // private String login = null;
    //public Protocol p;
    
    
    
    
    public OneThreadMock() throws IOException
    {
    }
    
    public OneThreadMock(Socket s) throws IOException
    {
        socket = s;
        in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket
                .getOutputStream())), true);
        
        Singleton.clients.add(p); 
        listener = new Proxy();
        
        start();
    }
    
    
      
    public void sendUser(String send)
    {
       out.println(send);
        System.out.println("Server out " + send);
    }
     
    
    public void run()
    {
        try
        {
            while (true)
            {
                String str = in.readLine();  
                System.out.println("Server comming: " + str);
                new MockReaction().get(str, this); /////////////////////
 
              if (str.equals("{\"Type\": \"End\"}")) // тут имя клиента
              {
               // Singleton.getInstance().delUser(login);
                out.println("{\"Type\": \"End\"}");
                break;
              }
                if(str.length() > 1){
                   str = "";
                }
            }
            System.out.println("closing...");
        }
        catch (IOException e)
        {
            System.err.println("IO Exception");
        }
        finally
        {
            try
            {
                socket.close();
            }
            catch (IOException e)
            {
                System.err.println("Socket not closed");
            }
        }
    }

    
    
}
